<%@page import="com.winexpress.product.Category"%>
<%@page import="com.winexpress.util.SessionAttributes"%>
<%@page import="java.util.List"%>
<%@page import="com.winexpress.product.Wine"%>
<%@page import="java.util.ArrayList"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>wineXpress Manage Wines</title>
    </head>
    <body>
        <t:adminNavigation/>
        <div class="container">
            <br/><br/>
            <h3 style="text-align: center;">Wine Management</h3>
            <div style="width:82%;margin-left: 10%">
                <form id="search" action="/WineXpressClient/searchManagement/manageWines" method="post">
                    <div style="width: 45%" class="col-lg-6">
                        <select class="form-control" name="search">
                            <option value="searchCategory">Search Category by Name</option>
                            <option value="searchByWineName">Search Wine by Wine name</option>
                            <option value="searcyByCategory">Search Wine by Category</option>
                            <option value="searchByCountry">Search Wine by Country</option>
                            <option value="searchByRegion">Search Wine by Region</option>
                            <option value="searchByVarietal">Search Wine by Varietal</option>
                            <option value="searchByVintage">Search Wine by Vintage</option>
                        </select>
                    </div>
                    <div style="float: left; width: 45%; padding-left: 10px; padding-right: 10px;" class="col-lg-6">
                        <input type="text" class="form-control" name="key" placeholder="Enter keywords">
                    </div>
                    <button type="submit" class="btn btn-default" style="width: 10%;">Search</button>
                </form>
            </div>

            <br><br>
            <%  Boolean isSearchCatgory = (Boolean) request.getAttribute("isSearchCatgory");
                if (isSearchCatgory != null && isSearchCatgory) { %>
            <%  List<Category> clist = (List) request.getAttribute("categoryList");%>
            <% if (!clist.isEmpty()) { %>
            <table class="table table-striped" style="width:84%; margin-left:8%">
                <thead>
                    <tr  class="info">
                        <th>Category</th>
                        <th> </th>
                    </tr>
                </thead>
                <% for (Object o : clist) {
                        Category c = (Category) o;%>
                <tr style="height: 10%">
                    <td style="width: 18%; vertical-align: middle;"> <a href="/WineXpressClient/searchManagement/manageWines?search=searcyByCategory&key=<%=c.getName()%>"><%=c.getName()%></a> </td>
                </tr>   
                <%}
                } else {%>
                <tr>
                <div class="alert alert-warning"style="width: 80%; margin-left: 10%; margin-right: 10%" >
                    Sorry, there are no such categories.<br> 
                    Please contact us by enquiries if you need further help.</div>             
                </tr>

                <%}%>
                </tbody > 
            </table >
            <%} else {%> 
            <div>
                <a href="/WineXpressClient/productManagement/addWine" style='margin-left: 11%;' class="btn btn-success" role="button">Add Wine</a>
            </div>
            <br/>
            <div>
                <% Boolean isDelete = (Boolean) request.getAttribute("isDelete");%>
                <% Boolean isRelist = (Boolean) request.getAttribute("isRelist");%>
                <% Boolean isAdd = (Boolean) request.getAttribute("isAdd");%>
                <% Boolean isEdit = (Boolean) request.getAttribute("isEdit");%>
                <%if (isDelete != null && isDelete) { %>
                <div class="alert alert-success"style="width: 75%; margin-left: 15%; margin-right: 10%" >The wine is deactivated.</div>
                <% } else if (isDelete != null && !isDelete) { %>
                <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error deactivating the wine.</div>
                <% } else if (isRelist != null && isRelist) { %>
                <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is relisted.</div>
                <% } else if (isRelist != null && !isRelist) { %>
                <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error relisting the wine.</div>
                <% } else if (isAdd != null && isAdd) {%>
                <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is added successfully.</div>
                <% } else if (isAdd != null && !isAdd) {%>
                <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error adding the wine</div>
                <% } else if (isEdit != null && isEdit) {%>
                <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is edited successfully.</div>
                <% } else if (isEdit != null && !isEdit) {%>
                <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error editing the wine</div>
                <% } %>
            </div>

            <div class="listWine">
                <table class="table table-hover" style='width:82%; margin-left: 11%; margin-right: 10%;'>
                    <thead>
                        <tr>
                            <th width="20">ID</th>
                            <th>Wine Name</th>
                            <th>Category</th>
                            <th>Stock</th>
                            <th>Is Active</th>
                            <th style='white-space: nowrap;' width="10"> </th>
                            <th style='white-space: nowrap;' width="15"> </th>
                            <th style='white-space: nowrap;' width="15"> </th>
                        </tr>
                    </thead>

                    <tbody>
                        <%  List<Wine> list = (List) session.getAttribute("winesList");
                            List<Category> categoryList = (List) session.getAttribute("wineCategoryList");%>
                        <% if (!list.isEmpty()) {
                                for (int i = 0; i < list.size(); i++) {
                                    Wine w = (Wine) list.get(i);
                                    Category c = (Category) categoryList.get(i);%>
                        <tr>
                            <td> <%=w.getId()%> </td>
                            <td> <%=w.getName()%> </td>
                            <td>  <%=c.getName()%> </td>
                            <td>  <%=w.getStock()%> </td>
                            <td>
                                <% if (w.isIsActive() == true) {
                                %>
                                <input type="checkbox"  name="isActive" value="true" checked disabled>
                                <% } else {
                                %> 
                                <input type="checkbox"  name="isActive" value="true" disabled>
                                <% }%>
                            </td>
                            <td align="right" style='white-space: nowrap;'> 
                                <form class="form-horizontal" role="form" id="editWine"  name="editWine" action="/WineXpressClient/productManagement/editWine" method="post">                         
                                    <% if (w.isIsActive() == true) {
                                    %>
                                    <button type="submit" class="btn btn-default" >Edit</button>                          
                                    <input name="edit" id="edit" type="hidden" value="loadEditWine"/>
                                    <input name="editWineID" id="editWineID" type="hidden" value="<%=w.getId()%>"/>
                                    <% } else {
                                    %> 
                                    <button type="submit" class="btn btn-default" disabled >Edit</button>                          
                                    <input name="edit" id="edit" type="hidden" value="loadEditWine"/>
                                    <input name="editWineID" id="editWineID" type="hidden" value="<%=w.getId()%>"/>
                                    <% }%>
                                </form> 
                            </td>
                            <td align="right" style='white-space: nowrap;'>
                                <form class="form-horizontal" role="form" id="deleteWine"  name="deleteWine" action="/WineXpressClient/productManagement/manageWines" method="post"> 
                                    <% if (w.isIsActive() == true) {
                                    %>
                                    <button type="submit" class="btn btn-default" >Delete</button>                          
                                    <input name="delete" id="delete" type="hidden" value="deleteWine"/>
                                    <input name="deleteID" id="deleteID" type="hidden" value="<%=w.getId()%>"/>
                                    <% } else {
                                    %> 
                                    <button type="submit" class="btn btn-default" disabled>Delete</button>                          
                                    <input name="delete" id="delete" type="hidden" value="deleteWine"/>
                                    <input name="deleteID" id="deleteID" type="hidden" value="<%=w.getId()%>"/>
                                    <% }%>                       
                                </form> 
                            </td>
                            <td align="right" style='white-space: nowrap;'>
                                <form class="form-horizontal" role="form" id="relistWine"  name="relistWine" action="/WineXpressClient/productManagement/manageWines" method="post"> 
                                    <% if (w.isIsActive() != true) {
                                    %>
                                    <button type="submit" class="btn btn-default" >Relist</button>                          
                                    <input name="relist" id="relist" type="hidden" value="relistWine"/>
                                    <input name="relistID" id="relistID" type="hidden" value="<%=w.getId()%>"/>
                                    <% } else {
                                    %> 
                                    <button type="submit" class="btn btn-default" disabled>Relist</button>                          
                                    <input name="relist" id="relist" type="hidden" value="relistWine"/>
                                    <input name="relistID" id="relistID" type="hidden" value="<%=w.getId()%>"/>
                                    <% }%>                       
                                </form> 
                            </td>
                        </tr>   
                        <%}
                        } else {%>
                        <tr>
                    <div class="alert alert-info"style="width: 75%; margin-left: 15%; margin-right: 10%" >No Wine in the database.</div>
                    </tr>
                    <%}%>

                    </tbody>
                </table>
            </div>
            <%}%>
        </div>
        <t:footer/>
    </body>
</html>
